package oss

import (
	"os"
	"os/signal"
	"sync"
	"syscall"
)

var (
	exitFunc []func()
	exitOnce sync.Once
)

// ListenExit 监听退出信号
func ListenExit(handler ...func()) {
	exitOnce.Do(func() {
		exitChan := make(chan os.Signal)
		signal.Notify(exitChan, os.Interrupt, os.Kill, syscall.SIGTERM)
		go func() {
			<-exitChan
			for _, v := range exitFunc {
				v()
			}
			os.Exit(-127)
		}()
	})
	exitFunc = append(exitFunc, handler...)
}

// Exit 退出程序
func Exit(code int) {
	os.Exit(code)
}

// Chmod 设置权限
func Chmod(path string, mode os.FileMode) error {
	return os.Chmod(path, mode)
}

// Wait 一直等待
func Wait() {
	select {}
}
